﻿/*
    File name:          UserLoginViewModel
    File type:          Class
    Author:             Mads Skipper Pedersen 07299
    Creation date:      19-04-2011
    Last modified:      18-05-2011
    Last modified by:   Mads Skipper Pedersen 07299
    Description:        The ViewModel for the Login view
                        Logs the user in and sends that information
                        to I4PrjViewModel (Main Views ViewModel)
*/

using System;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using i4pro_gruppe_d.Database;
using i4pro_gruppe_d.Types;

namespace UserLogin.ViewModel
{
    class UserLoginViewModel : ViewModelBase
    {
        #region Members
        private AbstractDatabase db;
        private Messenger MessengerInstance;
        #endregion
        #region Propertys

        private String username;
        public String Username
        {
            get
            {
                return username;
            }
            set
            {
                username = value;
                RaisePropertyChanged("Username");
            }
        }

        private String password;
        public String Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        private String loginStatusText;
        public String LoginStatusText
        {
            get
            {
                return loginStatusText;
            }
            set
            {
                loginStatusText = value;
                RaisePropertyChanged("LoginStatusText");
            }
        }

        private bool notLoggedIn;

        public bool NotLoggedIn
        {
            get
            {
                return notLoggedIn;
            }
            set
            {
                notLoggedIn = value;
                RaisePropertyChanged("NotLoggedIn");
            }
        }

        private String loginButtonContent;
        public String LoginButtonContent
        {
            get
            {
                return loginButtonContent;
            }
            set
            {
                loginButtonContent = value;
                RaisePropertyChanged("LoginButtonContent");
            }
        }
        #endregion
        #region Constructor
        public UserLoginViewModel()
        {
            db = AbstractDatabase.Instance;

            MessengerInstance = Messenger.Default;
            LoginStatusText = "Please enter username and password";
            
            Username = "";
            Password = "";
            loginButtonContent = "Login";
            NotLoggedIn = true;
        }
        #endregion
        #region Commands
        #region Login Command

        /// <summary>
        /// Run when user clicks login
        /// Runs the method LoginExecute()
        /// </summary>
        public ICommand LoginCmd
        {
            get { return new RelayCommand(LoginExecute, LoginCanExecute); }
        }

        /// <summary>
        /// Checks the entered username and password, sends it to the database and 
        /// gets a access level return, this access level is sent using message
        /// which is recieved by I4PrjViewModel.
        /// </summary>
        private void LoginExecute()
        {
            int rights = -1;
            User user = new User();

            if (LoginButtonContent != "Logout")
            {
                user.UserID = Username;
                user.Password = Password;

                rights = db.GetUserRights(user);

                switch (rights)
                {
                    case -1: //Login fail
                        LoginStatusText = "Login failed!";
                        NotLoggedIn = true;
                        break;
                    case 0: //Opperator
                        LoginButtonContent = "Logout";
                        LoginStatusText = "Logged in as Opperator";
                        NotLoggedIn = false;
                        break;
                    case 1: //Technitian
                        LoginButtonContent = "Logout";
                        LoginStatusText = "Logged in as Technician";
                        NotLoggedIn = false;
                        break;
                    case 2: //Admin
                        LoginButtonContent = "Logout";
                        LoginStatusText = "Logged in as Admin";
                        NotLoggedIn = false;
                        break;
                }
            }
            else
            {
                LoginButtonContent = "Login";
                LoginStatusText = "You are now logged out!";
                Username = "";
                Password = "";
                NotLoggedIn = true;
            }

            MessengerInstance.Send<User>(new User(Username, Password, rights));
        }

        /// <summary>
        /// Checks if username and password is empty
        /// </summary>
        /// <returns>Returns expression result</returns>
        private bool LoginCanExecute()
        {
            if (Username == "" && Password == "")
            {
                LoginStatusText = "Please enter Username and Password!";
                return false;
            }
            return true;
        }
        #endregion
        #endregion
    }
}
